Option name | Type | Description |
---|---|---|
credentials | Object | NetSuite credentials hash |
credentials.email | String | NetSuite user email |
credentials.password | String | NetSuite user password |
credentials.account | String | NetSuite company account id |
[credentials.role] | String | internal ID of role used to log in to NetSuite |
[options] | Object | options hash |
[options.apiVersion] | String | api version |
Represents configuration settings and helper functionality to connect to NetSuite.
var Configuration = module.exports = function Configuration(credentials, options) {
this.credentials = credentials || {};
this.options = _.merge({
apiVersion: '2014_2'
}, options);
// Default
this._webservicesDomain = 'https://webservices.na1.netsuite.com/';
};
Option name | Type | Description |
---|---|---|
credentials | Object | NetSuite credentials hash |
Static helper for creating NeSuite auth header
Configuration.createAuthHeader = function(credentials) {
// TODO: refactor into own class
var soapObj = {
'platformMsgs:passport': {
'platformCore:email': credentials.email,
'platformCore:password': credentials.password,
'platformCore:account': credentials.account
}
};
if (credentials.role) {
soapObj['platformMsgs:passport']['platformCore:role'] = {
$attributes: {
'internalId': credentials.role
}
};
}
return soapObj;
};
Option name | Type | Description |
---|---|---|
header | NetSuite.BaseObject | serializable header object |
Add SOAP header. This method does NOT replace existing SOAP headers with same name.
Configuration.prototype.addSoapHeader = function(header) {
if (!this.client || !this.client.soapHeaders) {
throw new Error('Client not initialized');
}
var soapHeader = Serializer.serialize(header);
this.client.addSoapHeader(soapHeader);
};
Option name | Type | Description |
---|---|---|
headerName | String | header to remove |
Remove all SOAP headers with given name
Configuration.prototype.removeSoapHeader = function(headerName) {
if (!this.client || !this.client.soapHeaders) {
return;
}
// This uses `node-soap` Client internals. Client maintains array
// of flattened (string) headers, so do a basic "startsWith" check
for (var i = this.client.soapHeaders.length - 1; i >= 0; i--) {
if (this.client.soapHeaders[i].indexOf('<' + headerName) === 0) {
this.client.soapHeaders.splice(i, 1);
}
}
};
Option name | Type | Description |
---|---|---|
[skipDiscovery=false] | Boolean | Don't try to discover WSDL url |
Create a NetSuite client using Configuration credentials and options
Configuration.prototype.createConnection = function(skipDiscovery) {
var _this = this;
var createClient = denodeify(soap.createClient);
var connect = function(resolve, reject) {
_this._resolveWsdl();
createClient(_this._wsdl, {
attributesKey: '$attributes'
})
.then(function(client) {
// Add all namespaces to SOAP envelope. Note this uses some private API methods
// TODO: subclass soap WSDL class
// TODO: detect and only add needed namespaces
_.assign(client.wsdl.definitions.xmlns, _this._namespaces);
client.wsdl.xmlnsInEnvelope = client.wsdl._xmlnsMap();
// Add auth soap header
var authHeader = Configuration.createAuthHeader(_this.credentials);
client.addSoapHeader(authHeader);
_this.client = client;
resolve(client);
})
.catch(function(err) {
reject(err);
});
};
return new Promise(function(resolve, reject) {
// Just connect with current settings if don't need to do discovery
if (skipDiscovery) {
connect(resolve, reject);
return;
}
// Retrieve appropriate service url from NetSuite REST service first
var authHeader = 'NLAuth nlauth_email=' + _this.credentials.email + ', nlauth_signature=' + _this.credentials.password;
https.get({
hostname: 'rest.netsuite.com',
path: '/rest/roles',
headers: {
'Authorization': authHeader
}
}, function(res) {
var buffer = [];
res.setEncoding('utf8');
res.on('data', function(chunk) {
buffer.push(chunk);
});
res.on('end', function() {
var json = JSON.parse(buffer.join(''));
var accountString = _this.credentials.account.toString();
// Result is array. Never seen more than 1 result, but if that happens,
// just use first matching result
var found = false;
for (var i = 0; i < json.length; i++) {
var config = json[i];
if (config.account && config.account.internalId === accountString) {
_this._webservicesDomain = config.dataCenterURLs.webservicesDomain;
found = true;
break;
}
}
if (!found) {
reject('Error resolving NetSuite datacenter');
return;
}
connect(resolve, reject);
});
}).on('error', function(err) {
reject(err);
});
});
};